package com.bwlcums.upload;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bwlcums.preview.service.FileListPreviewService;
import com.bwlcums.utils.StringUtils;
import com.bwlcums.utils.SystemPageWriter;

@Controller
@RequestMapping("/images/")
public class ImagesAction {

	@RequestMapping(value = "uploadPage")
	public String uploadPage(HttpServletRequest request,
			HttpServletResponse response) {
		return "shtml/html/account/html/jbzl";
	}

	@Resource
	FileListPreviewService fileListPreviewService;

	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "upload")
	public void upload(HttpServletRequest request, HttpServletResponse response) {
		String ip = StringUtils.getRemortIP(request);

		List<String> fileList = new ArrayList<>();
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload sfu = new ServletFileUpload(factory);
			sfu.setHeaderEncoding("UTF-8"); // 处理中文问题

			List<FileItem> fileItems = sfu.parseRequest(request); // 解码请求
																	// 得到所有表单元素
			for (FileItem fi : fileItems) {
				if (!fi.isFormField()) {
					Date now = new Date();
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyyMMddHHmmss");
					String ofilename = fi.getName();
					String filename = sdf.format(now)
							+ now.getTime()
							+ ofilename.substring(ofilename.lastIndexOf("."),
									ofilename.length());
					fileListPreviewService.saveFileInfo(ofilename, filename,
							filename, ip, fi.getSize(),
							IOUtils.toByteArray(fi.getInputStream()));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			String obj = "";
			for (int i = 0; i < fileList.size(); i++) {
				obj += fileList.get(i) + ";";
			}
			if (obj.length() > 0) {
				obj = obj.substring(0, obj.length() - 1);
			}

			SystemPageWriter.writeToPage(response, obj);
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "download")
	public void download(HttpServletRequest request,
			HttpServletResponse response) {
		String fname = request.getParameter("fname");
		byte[] bytes = fileListPreviewService.getImageData(fname);
		if (bytes != null) {
			try {
				response.setHeader("Content-Type", "image/jped");// 设置响应的媒体类型，这样浏览器会识别出响应的是图片
				response.setHeader("Content-Disposition", "attachment; filename=\"" + fname + "\"");
				response.getOutputStream().write(bytes);
				response.flushBuffer();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			SystemPageWriter.writeToPage(response, "文件不存在");
		}

	}
	
	@RequestMapping(value = "fileDelete")
	public void fileDelete(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		fileListPreviewService.deleteFile(id);
	}

}
